<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\Coupon;

/**
 * 折扣券 DAO
 *
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Dao
 */
class CouponDao extends Dao
{
    use DaoTrait;

    /**
     * 订单状态-完成
     *
     * @var int
     */
    const STATUS_COMPLETED = 2;

    /**
     * 全店打折
     *
     * @var int
     */
    const SHOP_DISCOUNT = 3;

    /**
     * 优惠券
     *
     * @var int
     */
    const COUPON = 1;

    /**
     * 现金抵用券
     *
     * @var int
     */
    const ENABLE_VOUCHER = 2;
    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return Coupon::class;
    }

    /**
     * 获取优惠券审核列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @return mixed
     */
    public function getExamineList(int $type, array $map)
    {
        return  $type == 3 ?
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('type', $type)
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture','status')
                ->paginate(10)
            :
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture','status','stock','expire_time','description','instructions')
                ->paginate(10)
            ;
    }

    /**
     * 获取优惠券全部信息
     * @return mixed
     */
    public function get()
    {
        return Coupon::query()->where('status',1)->select('business_id','id')->get();
    }

    /**
     * 获取优惠券列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @param array $Between 查询条件
     * @return mixed
     */
    public function getMainList(int $type, array $map, string $sort)
    {
        return  $type == 3 ?
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->limit(4)
                ->get()
            :
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->limit(4)
                ->get()
            ;
    }


    /**
     * 获取优惠券列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @param array $Between 查询条件
     * @param int $BetweenType
     * @return mixed
     */
    public function getListSearch(int $type, array $map, string $sort, array $Between, int $BetweenType)
    {
        return  $BetweenType == 1 ?
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->whereBetween('price',$Between)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            :
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            ;
    }

    /**
     * 获取优惠券列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @return mixed
     */
    public function getList(int $type, array $map, string $sort)
    {
        return  $type == 3 ?
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            :
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            ;

    }

    /**
     * 获取优惠券列表
     * @param int $type 类型
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @param array $Between 查询条件
     * @param int $BetweenType 查询条件
     * @return mixed
     */
    public function list(int $type, array $map, string $sort,array $Between, $BetweenType)
    {
        return  $BetweenType == 1 ?
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->whereBetween('price',$Between)
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            :
            Coupon::query()
                ->where(function ($query) use ($map) {
                    return queryWhere($query, $map);
                })
                ->where('status', 1)
                ->where('type', $type)
                ->where('expire_time', '>=', time())
                ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions')
                ->orderByRaw($sort)
                ->paginate(10)
            ;
    }

    /**
     * 优惠券详情
     * @param array $map
     * @return mixed
     */
    public function CouponDetails(array $map)
    {
        return Coupon::query()
            ->with('business:id,name,logo,address,tel,forenoon_hours,afternoon_hours,hours_describe')
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id', 'business_id', 'picture', 'title', 'sales', 'stock', 'goods_price', 'price', 'reach_price', 'description', 'expire_time', 'expire_type', 'created_at as created_time')
            ->first();
    }

    /**
     * 删除优惠券
     *
     * @param array $map 匹配条件
     * @return int
     */
    public function delete(array $map)
    {
        return Coupon::query()->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })->delete();
    }

    /**
     * 修改优惠券
     * @param array $map
     * @param array $data
     * @return mixed
     */
    public function update(array $map, array $data){
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->update($data);
    }

    /**
     * 通过条件查询单条数据
     *
     * @param array $map
     * @return mixed
     */
    public function getCouponByMap(array $map)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
            return queryWhere($query, $map);
        })
         ->select('*')
         ->first();
    }

    /**
     * 通过条件查询全店入驻数据
     *
     * @param array $map
     * @return mixed
     */
    public function getShopDiscountList(array $map , string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('status', 1)
            ->select('id', 'business_id', 'type', 'title', 'picture','category_id','describe')
            ->orderByRaw($sort)
            ->paginate(10);
    }

    /**
     * 根据类型查询券的列表
     *
     * @param array $map
     * @param string $sort
     * @return \Hyperf\Contract\LengthAwarePaginatorInterface
     */
    public function getCouponListByType (array $map, string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('*')
            ->orderByRaw($sort)
            ->paginate(10);
    }


    /**
     * 获取优惠券通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getCouponsByMapSelect(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();
        // 之间
        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }


    /**
     * 通过条件查询全店入驻数据
     * @param array $map
     * @param string $sort
     * @return \Hyperf\Contract\LengthAwarePaginatorInterface
     */
    public function getShopDiscount(array $map , string $sort)
    {
        return Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id', 'business_id', 'type', 'title', 'picture','category_id','describe','status')
            ->orderByRaw($sort)
            ->paginate(10);
    }

    /**
     * 获取优惠券列表
     * @param array $map 查询条件
     * @param string|null $sort 排序方式
     * @return mixed
     */
    public function getCouponList(array $map, string $sort)
    {
        return  Coupon::query()
            ->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id', 'business_id', 'type', 'title', 'description', 'price', 'goods_price','reach_price','picture', 'sales', 'stock',  'start_time','expire_time','instructions','describe','status')
            ->orderByRaw($sort)
            ->paginate(10);

    }

}